package org.rency.common.mq.request;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.rency.common.mq.enums.AcknowledgeMode;
import org.rency.common.mq.enums.DeliveryMode;
import org.rency.common.mq.enums.DestinationType;
import org.rency.common.mq.enums.MessageFormat;
import org.rency.common.mq.enums.MessageLevel;
import org.rency.common.mq.enums.NotifyType;

/**
 * 基本消息请求 主要包括创建Destination的基本信息，以及重试队列和失败队列等，应用中使用的请求应该继承本类。
 * 
 * @author: user_rcy@163.com </br>
 *          Created on 2016年4月8日
 * @version: V1.0.0
 * @since JDK 1.8
 */
public abstract class AbstraceRequest implements MQRequest {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2093177141868658414L;
	
	private Object content;

	/**
	 * 消息级别
	 */
	private MessageLevel messageLevel = MessageLevel.INFO;

	/**
	 * 目的地
	 */
	private String destination;

	/**
	 * 目的地类型，默认为QUEUE
	 */
	private DestinationType destinationType = DestinationType.QUEUE;

	/**
	 * 传输模式，默认为非持久化
	 */
	private DeliveryMode deliveryMode = DeliveryMode.NON_PERSISTENT;

	/**
	 * 应答模式，默认为自动应答
	 */
	private AcknowledgeMode acknowledgeMode = AcknowledgeMode.AUTO_ACKNOWLEDGE;

	/**
	 * 是否需要事务，默认为否
	 */
	private boolean transacted = false;

	/**
	 * 请求时间
	 */
	private Date requestTime = new Date();

	private NotifyType notifyType;

	private long expirationTime;

	private MessageFormat messageFormat = MessageFormat.OBJECT;

	private Map<String, Object> properties;

	public Object getContent() {
		return content;
	}

	public void setContent(Object content) {
		this.content = content;
	}

	public MessageLevel getMessageLevel() {
		return messageLevel;
	}

	public void setMessageLevel(MessageLevel messageLevel) {
		this.messageLevel = messageLevel;
	}

	public String getDestination() {
		return destination;
	}

	public void setDestination(String destination) {
		this.destination = destination;
	}

	public DestinationType getDestinationType() {
		return destinationType;
	}

	public void setDestinationType(DestinationType destinationType) {
		this.destinationType = destinationType;
	}

	public DeliveryMode getDeliveryMode() {
		return deliveryMode;
	}

	public void setDeliveryMode(DeliveryMode deliveryMode) {
		this.deliveryMode = deliveryMode;
	}

	public AcknowledgeMode getAcknowledgeMode() {
		return acknowledgeMode;
	}

	public void setAcknowledgeMode(AcknowledgeMode acknowledgeMode) {
		this.acknowledgeMode = acknowledgeMode;
	}

	public boolean isTransacted() {
		return transacted;
	}

	public void setTransacted(boolean transacted) {
		this.transacted = transacted;
	}

	public Date getRequestTime() {
		return requestTime;
	}

	public void setRequestTime(Date requestTime) {
		this.requestTime = requestTime;
	}

	public NotifyType getNotifyType() {
		return notifyType;
	}

	public void setNotifyType(NotifyType notifyType) {
		this.notifyType = notifyType;
	}

	public long getExpirationTime() {
		return expirationTime;
	}

	public void setExpirationTime(long expirationTime) {
		this.expirationTime = expirationTime;
	}

	public MessageFormat getMessageFormat() {
		return messageFormat;
	}

	public void setMessageFormat(MessageFormat messageFormat) {
		this.messageFormat = messageFormat;
	}

	public Map<String, Object> getProperties() {
		return properties;
	}

	public void setProperties(Map<String, Object> properties) {
		this.properties = properties;
	}

	public void addProperty(String key, Object value) {
		if (this.properties == null) {
			this.properties = new HashMap<String, Object>();
		}
		this.properties.put(key, value);
	}

	public Object getProperty(String key) {
		if (this.properties == null)
			return null;

		return this.properties.get(key);
	}

	public boolean containsProperty(String key) {
		if (this.properties == null)
			return false;

		return this.properties.containsKey(key);
	}

	protected void copyProperties(MQRequest dest) {
		if (dest == null){
			return;
		}

		dest.setContent(content);
		dest.setAcknowledgeMode(acknowledgeMode);
		dest.setDeliveryMode(deliveryMode);
		dest.setDestination(destination + "");
		dest.setDestinationType(destinationType);
		dest.setExpirationTime(expirationTime);
		dest.setMessageFormat(messageFormat);
		dest.setProperties(properties);
		dest.setRequestTime(requestTime);
		dest.setNotifyType(notifyType);
		dest.setTransacted(transacted);
	}

	@Override
	public String toString() {
		return "AbstractRequest [destination=" + destination + ", destinationType=" + destinationType
				+ ", deliveryMode=" + deliveryMode + ", acknowledgeMode=" + acknowledgeMode + ", transacted="
				+ transacted + ", requestTime=" + requestTime + ", notifyType=" + notifyType + ", expirationTime="
				+ expirationTime + ", messageFormat=" + messageFormat + ", properties=" + properties + "]";
	}

}